package com.huiying.controller;

import com.huiying.entity.dto.DemandDTO;
import com.huiying.entity.dto.DemandPageQueryDTO;
import com.huiying.entity.entity.Bid;
import com.huiying.entity.result.PageResult;
import com.huiying.entity.result.Result;
import com.huiying.entity.vo.DemandVO;
import com.huiying.service.DemandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: lsh
 * @Date: 2024/10/29/10:49
 * @Description: 需求单控制类
 */
@RestController
@Slf4j
@Api(tags = "需求单相关接口")
@RequestMapping("/user/demand")
public class DemandController {

    @Autowired
    private DemandService demandService;

    /**
     * 新增需求单
     * @param demandDTO 需求单数据传输对象
     * @return: com.fx.result.Result
     */
    @ApiOperation("新增需求单")
    @PostMapping ("/add")
    public Result add(@RequestBody DemandDTO demandDTO){
        log.info("新增需求单");
        demandService.add(demandDTO);
        return Result.success();
    }


    /**
     * 需求单分页查询
     * @param demandPageQueryDTO
     * @return: com.fx.result.Result<com.fx.result.PageResult>
     */
    @GetMapping("/page")
    @ApiOperation("需求单分页查询")
    public Result<PageResult> pageQuery(DemandPageQueryDTO demandPageQueryDTO){
        log.info("需求单分页查询{}",demandPageQueryDTO);

        PageResult pageResult = demandService.pageQuery(demandPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 用户发送接单请求
     * @param bid
     * @return: com.fx.result.Result
     */
    @PostMapping("/bid")
    @ApiOperation("用户接单")
    public Result bid(@RequestBody Bid bid){
        log.info("用户接单");
        Boolean b = demandService.bid(bid);
        return b?Result.success():Result.error("用户已接过该需求单");
    }


    /**
     * 根据id查询需求单
     * @param demandId
     * @return: com.fx.result.Result
     */
    @GetMapping("/getById")
    @ApiOperation("根据id查询需求单")
    public Result getById(Long demandId){
        log.info("查询需求单{}",demandId);
        DemandVO demandVO = demandService.getById(demandId);

        return Result.success(demandVO);
    }

    /**
     * 修改需求单
     * @param demandDTO
     * @return: com.fx.result.Result
     */
    @PutMapping("/update")
    @ApiOperation("修改需求单")
    public Result update(@RequestBody DemandDTO demandDTO){
        log.info("修改需求单");
        demandService.updateDemand(demandDTO);
        return Result.success();
    }

    @DeleteMapping("/delete/{demandId}")
    @ApiOperation("删除需求单")
    public Result delete(@PathVariable Long demandId){
        log.info("删除需求单{}",demandId);
        demandService.deleteDemand(demandId);
        return Result.success();
    }

}
